﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 仪表识别2
{
    class Log
    {
        public static RichTextBox richTextBox;
        //级别 0 debug 1 info 2 warn 3 error
        public static int leve = 1;
        
        #region 日志记录、支持其他线程访问 
        public delegate void LogAppendDelegate(Color color, string text);
        /// <summary> 
        /// 追加显示文本 
        /// </summary> 
        /// <param name="color">文本颜色</param> 
        /// <param name="text">显示文本</param> 
        public static void LogAppend(Color color, string text)
        {
            richTextBox.AppendText("\n");
            richTextBox.SelectionColor = color;
            richTextBox.AppendText(text);
            richTextBox.ScrollToCaret();

        }
        /// <summary> 
        /// 显示错误日志 
        /// </summary> 
        /// <param name="text"></param> 
        public static void error(string text)
        {
            if (leve <= 3)
            {
                StackTrace st = new StackTrace(true);
                StackFrame sf = st.GetFrame(1);
                LogAppendDelegate la = new LogAppendDelegate(LogAppend);
                string className = sf.GetFileName();
                className = className.Substring(className.LastIndexOf("\\")+1);
                string methodName = sf.GetMethod().Name;
                int lineNum = sf.GetFileLineNumber();
                richTextBox.Invoke(la, Color.Red, DateTime.Now.ToString("yyy-mm-dd HH:mm:ss ") + " [ERROR] " + "[" + className + ":" + lineNum + "]\t" + text);

            }
        }
        /// <summary> 
        /// 显示警告信息 
        /// </summary> 
        /// <param name="text"></param> 
        public static void warn(string text)
        {
            if (leve <= 2)
            {
                StackTrace st = new StackTrace(true);
                StackFrame sf = st.GetFrame(1);
                LogAppendDelegate la = new LogAppendDelegate(LogAppend);
                string className = sf.GetFileName();
                className = className.Substring(className.LastIndexOf("\\") + 1);
                string methodName = sf.GetMethod().Name;
                int lineNum = sf.GetFileLineNumber();
                richTextBox.Invoke(la, Color.Orange, DateTime.Now.ToString("yyy-mm-dd HH:mm:ss ") + " [WARN] " + "[" + className + ":" + lineNum + "]\t" + text);
            }
        }
        /// <summary> 
        /// 显示信息 
        /// </summary> 
        /// <param name="text"></param> 
        public static void info(string text)
        {
            if (leve <= 1)
            {
                StackTrace st = new StackTrace(true);
                StackFrame sf = st.GetFrame(1);
                LogAppendDelegate la = new LogAppendDelegate(LogAppend);
                string className = sf.GetFileName();
                className = className.Substring(className.LastIndexOf("\\") + 1);
                string methodName = sf.GetMethod().Name;
                int lineNum = sf.GetFileLineNumber();
                richTextBox.Invoke(la, Color.Black, DateTime.Now.ToString("yyy-mm-dd HH:mm:ss ") + " [INFO] " + "["+ className + ":" + lineNum + "]\t" + text);
            }
        }
        /// <summary> 
        /// 显示debug信息 
        /// </summary> 
        /// <param name="text"></param> 
        public static void debug(string text)
        {
            if (leve <= 0)
            {
                StackTrace st = new StackTrace(true);
                StackFrame sf = st.GetFrame(1);
                LogAppendDelegate la = new LogAppendDelegate(LogAppend);
                string className = sf.GetFileName();
                className = className.Substring(className.LastIndexOf("\\") + 1);
                string methodName = sf.GetMethod().Name;
                int lineNum = sf.GetFileLineNumber();
                richTextBox.Invoke(la, Color.Blue, DateTime.Now.ToString("yyy-mm-dd HH:mm:ss ") + " [DEBUG] " + "[" + className + ":" + lineNum + "]\t" + text);
            }
        }

        public static void clean()
        {
            richTextBox.Text = "";
        }
        #endregion
    }
}
